﻿/*
	Copyright (C) 2019 Zongming Liu <1849059316@qq.com>

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, write to the Free Software Foundation, Inc.,
	51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#ifndef UiCore_h__
#define UiCore_h__

#include "../utility/Singleton.h"
#include "Widget.h"
#include "../utility/SmartPtrDef.h"

namespace SkyEngine2d
{
	
	class EventKey;
	class EventMouse;
	class MouseCursor;
	class Camera;
	class GameWorld;

	/*!
	 \brief UI 核心
	 \details
	*/
	class SGE_API UiCore
		:public Singleton<UiCore,false>
	{
		SINGLETON_CLASS(UiCore)
	public:
		//! 析构
		~UiCore();
		//! 触发Ui系统的 键盘 按下事件
		/*!
		\param EventKey * event
		\return bool
		*/
		bool onKeyPressEvent(EventKey* event);
	
		//! 触发UI系统的 键盘 释放事件 
		/*!
		\param EventKey * event
		\return bool
		*/
		bool onKeyReleaseEvent(EventKey* event);
	
		//! 触发UI系统的鼠标按下事件
		/*!
		\param EventMouse* event
		\return bool
		*/
		bool onMousePressEvent(EventMouse* event);
	
		//! 触发UI系统的鼠标释放事件
		/*!
		\param EventMouse* event
		\return bool
		*/
		bool onMouseReleaseEvent(EventMouse* event);
	
		//! 触发Ui系统的鼠标移动事件
		/*!
		\param EventMouse* event
		\return bool
		*/
		bool onMouseMoveEvent(EventMouse* event);
	
		//! 触发UI系统的 鼠标双击事件
		/*!
		\param EventMouse* event
		\return bool
		*/
		bool onMouseDoubleClickedEvent(EventMouse* event);
		
		//! 触发UI系统的 字符输入事件
		/*!
		\param EventCharacter * e 
		\return bool 
		*/
		bool onCharacterEvent(EventCharacter* e);
		
		//! 返回包含鼠标的窗体
		/*!
		\return Widget*
		*/
		Widget* getWidgetContainingMouse(EventMouse* event);
	
		//! 获取默认相机
		/*!
		\return Camera*  
		*/
		Camera* getDefaultCamera()const;
	
		//! 获取当前的默认窗口
		/*!
		\return Widget*  
		*/
		Widget* getDefaultWidget();
		
		//! 设置默认的窗口
		/*!
		\param Widget * widget 
		\return void 
		*/
		void setDefaultWidget(Widget* widget);
		
		//! 获取当前捕获的窗口
		/*!
		\return Widget* 
		*/
		Widget* getInputCaptureWidget()const;
		
		//! 设置当前捕获的窗口
		/*!
		\param Widget * widget 
		\return void  
		*/
		void setInputCaptureWidget(Widget* widget);
	
	protected:
		//! 构造
		UiCore();
	
		//! 更新包含鼠标的窗口
		/*!
		\return bool
		*/
		bool updateWidgetContainingMouse(EventMouse* event);
	
		//! 获取某个坐标对应的UI控件
		/*!
		\param const Vector2 & pos  坐标位置
		\param const bool allowDisabled 是否允许计算 禁用的控件
		\return Widget*  
		*/
		Widget* getTargetWidgetByPos(const Vector2& pos, const bool allowDisabled);
	
	private:
		Widget*							m_rootWidget; /*!< 所有窗口的根窗口. */
		TextFontSharedPtr				m_defaultFont;  /*!< 默认的字体. */
		std::shared_ptr<MouseCursor>    m_mouseCursor;/*!< 鼠标光标. */
		Widget*							m_widgetContainingMouse; /*!< 当前鼠标下面的窗口. */
		bool							m_widgetContainingMouseIsUpTodate; /*!< 包含鼠标的窗口是否是最新的. */
		Widget*							m_inputCaptureWidget;/*!< 捕获输入的窗口. */
		SceneSharedPtr					m_currentScence; /*!< 当前的场景. */
		Camera*							m_uiCamera;     /*!< ui系统的默认相机. */

		friend class GameWorld;
	};
}

#endif // UiCore_h__
